package com.fuhao.member.service;

import com.fuhao.core.Constants.Constants;
import com.fuhao.core.base.BaseApiService;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.core.utils.BeanUtil;
import com.fuhao.core.utils.GenerateToken;
import com.fuhao.core.utils.MD5Util;
import com.fuhao.core.utils.TypeCastHelper;
import com.fuhao.dto.input.MemberLoginInpDTO;
import com.fuhao.dto.output.MemberOutDTO;
import com.fuhao.member.entity.MemberEntity;
import com.fuhao.member.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class IsExistMemberServiceImpl extends BaseApiService<MemberOutDTO> implements ExixeMemberService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private GenerateToken generateToken;

	/**
	 * 验证用户是否已注册
	 * @param mobile
	 * @return
	 */
	@Override
	public BaseResponse<MemberOutDTO> existMobile(String mobile) {
		// 1.验证参数
		if (StringUtils.isEmpty(mobile)) {
			return setResultError("手机号码不能为空!");
		}
		// 2.根据手机号码查询用户信息 单独定义code 表示是用户信息不存在把
		MemberEntity userEntity = userMapper.existMobile(mobile);
		if (userEntity == null) {
			return setResultError(Constants.HTTP_RES_CODE_MOBILE_203, "用户信息不存在!");
		}
		MemberOutDTO memberOutDTO = BeanUtil.doToDto(userEntity, MemberOutDTO.class);
		return setResultSuccess(memberOutDTO);
	}

	/**
	 * 根据token查询用户
	 * @param token
	 * @return
	 */
	@Override
	public BaseResponse<MemberOutDTO> getInfo(String token) {
		if (StringUtils.isEmpty(token)){
			return setResultError("token不能为空！");
		}
		String tokenValue = generateToken.getToken(token);
		if(StringUtils.isEmpty(tokenValue)){
			return setResultError("Token错误或者过期！");
		}
		Long userId = TypeCastHelper.toLong(tokenValue);
		MemberEntity memberEntity = userMapper.findByUserId(userId);
		if(memberEntity == null){
			setResultError("未找到用户信息");
		}
		// 4.将Do转换为Dto
		MemberOutDTO doToDto = BeanUtil.doToDto(memberEntity, MemberOutDTO.class);
		return setResultSuccess(doToDto);
	}

	@Override
	public BaseResponse<MemberOutDTO> ssoLogin(@RequestBody MemberLoginInpDTO memberLoginInpDTO) {
		// 1.验证参数
		String mobile = memberLoginInpDTO.getMobile();
		if (StringUtils.isEmpty(mobile)) {
			return setResultError("手机号码不能为空!");
		}
		String password = memberLoginInpDTO.getPassword();
		if (StringUtils.isEmpty(password)) {
			return setResultError("密码不能为空!");
		}
		// 判断登陆类型
		String loginType = memberLoginInpDTO.getLoginType();
		if (StringUtils.isEmpty(loginType)) {
			return setResultError("登陆类型不能为空!");
		}
		// 目的是限制范围
		if (!(loginType.equals(Constants.MEMBER_LOGIN_TYPE_ANDROID) || loginType.equals(Constants.MEMBER_LOGIN_TYPE_IOS)
				|| loginType.equals(Constants.MEMBER_LOGIN_TYPE_PC))) {
			return setResultError("登陆类型出现错误!");
		}

		// 设备信息
		String deviceInfor = memberLoginInpDTO.getDeviceInfor();
		if (StringUtils.isEmpty(deviceInfor)) {
			return setResultError("设备信息不能为空!");
		}
		// 2.对登陆密码实现加密
		String newPassWord = MD5Util.MD5(password);
		// 3.使用手机号码+密码查询数据库 ，判断用户是否存在
		MemberEntity userDo = userMapper.login(mobile, newPassWord);
		if (userDo == null) {
			return setResultError("用户名称或者密码错误!");
		}
		return setResultSuccess(BeanUtil.doToDto(userDo, MemberOutDTO.class));

	}


}
